<?php
	if (isset($_POST['name']))
	{
  	$id = mysql_connect($db_host,$db_usr,$db_pass) or die('Could not connect: ' . mysql_error());
  	mysql_select_db($db_name);
  	
  	$i=1;   
  	$add = "./../images/gallery/".$i.".jpg";
  	
  	while (file_exists($add))
  	{
  		$i++;
  		$add="./../images/gallery/".$i.".jpg";
  	}
  	
  	if(move_uploaded_file($_FILES['gallery']['tmp_name'],$add))
  		{
  		echo "Successfully uploaded the image";
  		
  		$query="INSERT INTO gallery (name,link) VALUES ('".$_POST['name']."','".$i.".jpg')";
  		
  		mysql_query($query,$id);
  
  		mysql_close($id);
  		
  		
  			$n_width=128;
  			$n_height=96;
  			$tsrc="./../images/gallery/thumbnails/".$i.".jpg";
  			$im = imagecreatefromjpeg($add);
  			$width = imagesx($im);
  			$height = imagesy($im);
  			$newimage = imagecreatetruecolor($n_width,$n_height); 
  			imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
  			imagejpeg($newimage,$tsrc);
  		}
	}
	else
	{
	echo "<form enctype='multipart/form-data' method='post'><br>
	<input type='text' name='name'><br>
	<input type='file' name='gallery'><br>
	<input type='hidden' name='edit' value='add'>
	<input type='submit'>
	</form>";
	}
?>
